/**
 * Created by AllanXu on 01/03/2016.
 */
define(function (require) {
    var Vue = require('vue'),
        Config = require('config'),
        _ = require('ajax'),
        utilities = require('utilities'),
        id = sessionStorage.id;
    //var userInfo = JSON.parse(decodeURIComponent(atob(sessionStorage.userInfo)));

    var app = new Vue({
        el: '#uc_mate_condition',
        data: {
            sexList: [],
            placeList: [],
            marriageList: [],
            educationList: [],
            cityList: [],
            cityList1: [],
            detail: {},
            ageArr: [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45],
            moneyArr: [2000, 4000, 6000, 8000, 10000, 12000, 14000, 16000, 18000, 20000, 30000, 50000, 100000],
            heightArr: [154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178,180,182,184,186,188,190],
            req: {
                region: '',
                // sex: '',
                place: '',
                marrys: '',
                ageBegin: '',
                ageEnd: '',
                moneyBegin: '',
                moneyEnd: '',
                heightBegin: '',
                heightEnd: '',
                educations: ''
            },
            region: {
                cregion_id: 0,
                xregion_id: 0,
                cityList: [],
                regionList: []
            }
        },
        methods: {
            loadArea: function (arrName, code, callback) {
                _(Config.SERVICE.COMMON.ADDRLIST, {
                    parentId: code
                }, function (data) {
                    app.$set(arrName, data.obj[code]);
                    app.$get(arrName).unshift({
                        id: '',
                        name: '请选择'
                    });
                    if (arrName === 'region.regionList') {
                        app.region.xregion_id = '';
                    }
                    if (typeof callback === 'function') {
                        callback();
                    }
                });
            },
            init: function () {
                app.mate = utilities.base64TojsonObj(sessionStorage.userInfo).mate;

                for (var key in app.req) {
                    if (app.mate[key] == 0) {
                        app.req[key] = '';
                    } else {
                        app.req[key] = app.mate[key];
                    }
                }

                _(Config.DICTIONARY, {
                    parentId: '340,6,8,975'
                }, function (data) {
                    //console.log(data);
                    app.sexList = data.obj[340];
                    app.placeList = data.obj[975];
                    app.placeList.unshift({
                        id: '',
                        name: '不限'
                    });
                    var marriageList = app.removeUnused(data.obj[6]);
                    var educationList = app.removeUnused(data.obj[8]);

                    utilities.restoreCheckList(marriageList, app.req.marrys.split(','));
                    utilities.restoreCheckList(educationList, app.req.educations.split(','));

                    app.marriageList = marriageList;
                    app.educationList = educationList;
                });

                _(Config.SERVICE.COMMON.ADDRLIST, {
                    parentId: 2
                }, function (data) {
                    var cityList = data.obj[2];
                    utilities.restoreCheckList(cityList, app.req.region.split(','));
                    app.cityList = cityList;
                });

                _(Config.SERVICE.COMMON.ADDRLIST, {
                    parentId: 143666
                }, function (data) {
                    var cityList1 = data.obj[143666];
                    utilities.restoreCheckList(cityList1, app.req.region.split(','));
                    app.cityList1 = cityList1;
                });

            },
            removeUnused: function (arr) {
                var _arr = [];
                for (var i = 0; i < arr.length; i++) {
                    if (arr[i].name != '不透露') {
                        _arr.push(arr[i]);
                    }
                }
                return _arr;
            },
            save: function () {
                for (var key in app.req) {
                    if(key!='place' && key!='region'&&key!='marrys' &&key!='educations'){
                        if (app.req[key] === ''){
                            alert(key);
                            alert('请填写完整！');
                            return;
                        }
                    }
                }

                var req = {};
                for (var key in app.req) {
                    //console.log(key); // for in 用于遍历对象、打印出对象属性
                    req['xgmate.' + key] = app.req[key];
                }

                req['xgmate.marrys'] = utilities.getCheckedStr(app.marriageList);
                req['xgmate.educations'] = utilities.getCheckedStr(app.educationList);
                req['xgmate.region'] = utilities.getCheckedStr(app.cityList);
                var szjg=utilities.getCheckedStr(app.cityList1);
                if(szjg!='')
                req['xgmate.region']+=','+szjg;
                // req['xgmate.region'] = app.region.xregion_id;

                req.id = id;

                _(Config.SERVICE.USERCENTER.MATE_CONDITION_SAVE, req, function (data) {
                    if (data.code == 200 && data.type === 'SUCCESS') {
                        alert('保存成功！');
                        utilities.refreshUserInfo(function (data) {
                            history.back();
                        });
                    } else {
                        alert(data.errmsg);
                    }
                });
            },
            go: function (url) {
                location.href = url;
            },
            isChecked: function (item) {
                return !!item.checked;
            },
            pick: function (listName, i) {
                app.$set(listName + '[' + i + '].checked', !app[listName][i].checked);
            },
            getName: function (arr, id) {
                for (var i = 0; i < arr.length; i++)
                    if (arr[i]['id'] == id)
                        return arr[i]['name'];
            }
        }
    });
    app.init();
});